<?php
class Model_telesched extends Model {
    function Model_telesched() {
        parent::Model();
        $this->aUser = $this->usersession->currentUser();
    }

    /**     Functie care aduce datele in grid
     * @param  $aPost
     * @return array
     */
    function getData($aPost) {

        $iStart = (isset($aPost["start"]) && ($aPost["start"] != "")) ? $aPost["start"] : 0;
        $iLimit = (isset($aPost["limit"]) && ($aPost["limit"] != "")) ? $aPost["limit"] : 100;
        $sSort = (isset($aPost["sort"]) && ($aPost["sort"] != "")) ? $aPost["sort"] : "tsched_date";
        $sDir = (isset($aPost["dir"]) && ($aPost["dir"] != "")) ? $aPost["dir"] : "desc";

        if (!isset($aPost["client_persons_id"])) {
            return array();
        }

        $this->db->where("tele_schedule.client_persons_id", $aPost["client_persons_id"]);
        $this->db->join("position", "position.position_id=tele_schedule.position_id", "left");
        $this->db->order_by($sSort, $sDir);
        $this->db->limit($iLimit, $iStart);
        $query = $this->db->get("tele_schedule");

        $totalCount = $this->db->count_all_results("tele_schedule");
        $aData = $query->result_array();

        $data = array('totalCount' => $totalCount, 'data' => $aData);
        return $data;
    }


    /**
     * Adaugare / editare
     * @param  $aData
     * @return bool|string
     */
    function operation($aData) {
        $aTemp = array(
            "tsched_name" => $aData["tsched_name"],
            "tsched_phone" => $aData["tsched_phone"],
            "tsched_email" => $aData["tsched_email"],
            "tsched_place" => $aData["tsched_place"],
            "tsched_company" => $aData["tsched_company"],
            "tsched_company_adr" => $aData["tsched_company_adr"],
            "tsched_date" => $aData["tsched_date"],
            "position_id" => $aData["position_id"],
            "client_persons_id" => $aData["client_persons_id"]
        );
        //insert
        if ($aData["tsched_id"] == 0) {
            $this->db->insert("tele_schedule", $aTemp);
            $iProgId = $this->db->insert_id();
            if ($this->db->affected_rows() > 0) {
                $this->insertLog($aData["client_persons_id"], "Adaugare programare nr ".$iProgId, "prog_add");
            }
        }
        else {
            $this->db->where("tsched_id", $aData["tsched_id"]);
            $this->db->update("tele_schedule", $aTemp);
            if ($this->db->affected_rows() > 0) {
                $this->insertLog($aData["client_persons_id"], "Modificare programare nr ". $aData["tsched_id"], "prog_mod");
            }
        }
        return TRUE;
    }

    /**
     * Functie care aduce datele despre o inregistrare
     */
    function getRecord($aData) {
        $this->db->select("tele_schedule.*, name as position_id_val");
        $this->db->join("position", "position.position_id=tele_schedule.position_id", "left");
        $this->db->where("tsched_id", $aData["iRec"]);
        return $this->db->get("tele_schedule")->row_array();
    }

     /**
     * Salvare log
     * @param  $iContact
     * @param  $sOperation
     * @return void
     */
    function insertLog($iContact, $sOperationText, $sOperation) {
        $this->db->set("user_id", $this->aUser["user_id"]);
        $this->db->set("client_persons_id", $iContact);
        $this->db->set("tl_operation", $sOperation);
        $this->db->set("tl_operation_text", $sOperationText);
        $this->db->insert("tele_log");
    }

}